<!-- 个人信息 -->
<template>
  <div class="app-container">
    <el-form
      ref="ucentermember"
      :rules="rules"
      :model="ucentermember"
      label-width="80px"
    >
      <!-- 用户头像 -->
      <el-form-item label="头像">
        <el-upload
          class="avatar-uploader"
          :action="action"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img
            v-if="ucentermember.avatar"
            :src="ucentermember.avatar"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <!-- 昵称 -->
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="ucentermember.nickname"
          placeholder="请输入昵称"
        ></el-input>
        <el-button
          round
          type="primary"
          icon="el-icon-edit"
          @click="modifynickname(ucentermember.id, ucentermember.nickname)"
          >修改昵称</el-button
        >
      </el-form-item>

      <!-- 签名 -->
      <el-form-item label="个人签名" prop="sign">
        <el-input
          v-model="ucentermember.sign"
          placeholder="请输入签名"
        ></el-input>
        <el-button
          round
          type="primary"
          icon="el-icon-edit"
          @click="modifynicksign(ucentermember.id, ucentermember.sign)"
          >修改签名</el-button
        >
      </el-form-item>

      <!-- 性别 -->
      <el-form-item label="性别">
        <el-radio-group v-model="ucentermember.sex">
          <el-radio :label="2">男</el-radio>
          <el-radio :label="1">女</el-radio>
          <el-button
            round
            type="primary"
            icon="el-icon-edit"
            @click="modifynicksex(ucentermember.id, ucentermember.sex)"
            >修改性别</el-button
          >
        </el-radio-group>
      </el-form-item>

      <!-- 年龄 -->
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="ucentermember.age"
          onkeyup="this.value = this.value.replace(/[^\d.]/g,'');"
        ></el-input>
        <el-button
          round
          type="primary"
          icon="el-icon-edit"
          @click="modifynickage(ucentermember.id, ucentermember.age)"
          >修改年龄</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import contestloginApi from "@/api/contestlogin";
import ucenterMemberApi from "@/api/ucenter/ucenterMember";

export default {
  data() {
    return {
      // 存放用户信息
      ucentermember: {},
      // 上传头像地址
      action: process.env.BASE_API + "/fileoss/sjoss",
      // 表单校验
      rules: {
        nickname: [
          { required: true, message: "请输入昵称", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        sign: [
          { required: true, message: "请输入签名", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
        age: [
          { required: true, message: "请输入年龄", trigger: "blur" },
          {
            min: 1,
            max: 2,
            message: "如果您一百多岁，请联系管理员，我们将为您发放特殊福利",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    info() {
      // 查询用户信息
      contestloginApi
        .getMemberInfo(this.$cookie.get("token"))
        .then((response) => {
          this.ucentermember = response.data.userInfo;
        });
    },
    // 头像上传成功的钩子
    handleAvatarSuccess(response, file, fileList) {
      // 更改数据库头像信息
      ucenterMemberApi
        .updateAvatar(this.ucentermember.id, response.data.url)
        .then((response) => {
          if (response.code !== 200) return;
          location.reload();
          this.$message({
            message: "更换头像成功",
            type: "success",
          });
        });
    },
    // 头像上传之前的钩子
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 修改昵称
    modifynickname(id, nickname) {
      this.$refs.ucentermember.validateField("nickname", (valid) => {
        if (valid) return;
        const ucenterMember = { id, nickname };
        ucenterMemberApi
          .updateUcenterMemberById(ucenterMember)
          .then((response) => {
            if (response.code != 200) return;
            this.$message({
              message: "修改昵称成功",
              type: "success",
            });
            // 刷新页面emmm，技术有限先暂时这样，以后更改 TODO
            setTimeout(() => {
              location.reload();
            }, 1000);
          });
      });
    },
    // 修改签名
    modifynicksign(id, sign) {
      this.$refs.ucentermember.validateField("sign", (valid) => {
        if (valid) return;
        const ucenterMember = { id, sign };
        ucenterMemberApi
          .updateUcenterMemberById(ucenterMember)
          .then((response) => {
            if (response.code != 200) return;
            this.$message({
              message: "修改签名成功",
              type: "success",
            });
            // 刷新页面emmm，技术有限先暂时这样，以后更改 TODO
            setTimeout(() => {
              location.reload();
            }, 1000);
          });
      });
    },
    // 修改性别
    modifynicksex(id, sex) {
      const ucenterMember = { id, sex };
      ucenterMemberApi
        .updateUcenterMemberById(ucenterMember)
        .then((response) => {
          if (response.code != 200) return;
          this.$message({
            message: "修改性别成功",
            type: "success",
          });
          // 刷新页面emmm，技术有限先暂时这样，以后更改 TODO
          setTimeout(() => {
            location.reload();
          }, 1000);
        });
    },
    // 修改年龄
    modifynickage(id, age) {
      this.$refs.ucentermember.validateField("age", (valid) => {
        if (valid) return;
        const ucenterMember = { id, age };
        ucenterMemberApi
          .updateUcenterMemberById(ucenterMember)
          .then((response) => {
            if (response.code != 200) return;
            this.$message({
              message: "修改年龄成功",
              type: "success",
            });
            // 刷新页面emmm，技术有限先暂时这样，以后更改 TODO
            setTimeout(() => {
              location.reload();
            }, 1000);
          });
      });
    },
  },
  created() {
    // 显示用户属性值
    this.info();
  },
};
</script>
<style scoped>
.el-input {
  width: 200px;
}
/* 用户头像上传 */
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>